package com.felix.netty.c1.future;

import io.netty.channel.DefaultEventLoop;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;


/** 同步处理任务失败 - sync & get **/
@Slf4j
public class example3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        DefaultEventLoop eventExecutors = new DefaultEventLoop();
        DefaultPromise<Object> promise = new DefaultPromise<>(eventExecutors);

        eventExecutors.execute(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            RuntimeException e = new RuntimeException("error....");
            log.debug("set failure, {}", e.toString());
            promise.setFailure(e);
        });

        log.debug("start...");
        log.debug("{}",promise.getNow());
        promise.get();
//        promise.sync(); sync() 也会出现异常，只是 get 会再用 ExecutionException 包一层异常

//        00:14:17 [DEBUG] [main] c.f.n.c.f.example3 - start...
//        00:14:17 [DEBUG] [main] c.f.n.c.f.example3 - null
//        00:14:18 [DEBUG] [defaultEventLoop-1-1] c.f.n.c.f.example3 - set failure, java.lang.RuntimeException: error....
//        Exception in thread "main" java.lang.RuntimeException: error....
//        at com.felix.netty.c1.future.example3.lambda$main$0(example3.java:24)
//        at io.netty.channel.DefaultEventLoop.run(DefaultEventLoop.java:54)
//        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918)
//        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
//        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
//        at java.base/java.lang.Thread.run(Thread.java:833)

//        00:14:42 [DEBUG] [main] c.f.n.c.f.example3 - start...
//        00:14:42 [DEBUG] [main] c.f.n.c.f.example3 - null
//        00:14:43 [DEBUG] [defaultEventLoop-1-1] c.f.n.c.f.example3 - set failure, java.lang.RuntimeException: error....
//        Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.RuntimeException: error....
//        at io.netty.util.concurrent.AbstractFuture.get(AbstractFuture.java:41)
//        at com.felix.netty.c1.future.example3.main(example3.java:31)
//        Caused by: java.lang.RuntimeException: error....
//        at com.felix.netty.c1.future.example3.lambda$main$0(example3.java:24)
//        at io.netty.channel.DefaultEventLoop.run(DefaultEventLoop.java:54)
//        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918)
//        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
//        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
//        at java.base/java.lang.Thread.run(Thread.java:833)
    }
}
